﻿using System;
using System.Collections.Generic;
using UnityEngine;
using LitJson;

namespace Pumpkin.FrameBase.Resource
{
    public class ResLoader : IResLoader
    {
        public void Init()
        {
#if UNITY_EDITOR
            if (HotFixMain.Config.EnableHotFix)
            {
                BundleManager.Instance.Init();
                SetBundles();
            }
#else
            BundleManager.Instance.Init();
            SetBundles();
#endif
        }

        private void SetBundles()
        {
            var sceneResData = LoadAsset<UnityEngine.Object>(HotFixConfigs.SceneResMgrFile, true);
            if (sceneResData == null)
            {
                return;
            }

            var sceneResMap = JsonMapper.ToObject<Dictionary<string, List<string>>>(sceneResData.ToString());
            foreach (var sceneItem in sceneResMap)
            {
                if (sceneItem.Key == "common")
                {
                    BundleManager.Instance.SetCommonBundles(sceneItem.Value);
                }
                else
                {
                    BundleManager.Instance.SetSceneBundles(sceneItem.Key, sceneItem.Value);
                }
            }
        }

        public T LoadAsset<T>(string path, bool autoUnload = false) where T : UnityEngine.Object
        {
#if UNITY_EDITOR
            if (HotFixMain.Config.EnableHotFix)
            {
                return BundleManager.Instance.LoadAssetFromBundle<T>(path, autoUnload);
            }
            else
            {

                var realPath = FileHelper.GetABResPath(path);
                return UnityEditor.AssetDatabase.LoadAssetAtPath<T>(realPath);
            }
#else
            return BundleManager.Instance.LoadAssetFromBundle<T>(path, autoUnload);
#endif
        }

        public UnityEngine.Object LoadAsset(string path, Type type)
        {
            throw new System.NotSupportedException("ResLoad.LoadAsset(string, Type) not implemented");
            // #if UNITY_EDITOR
            //             if (HotFixMain.Config.EnableHotFix)
            //             {
            //                 throw new System.NotSupportedException("ResLoader.LoadAsset");
            //             }
            //             else
            //             {
            // 				var realPath = FileHelper.GetABResPath(path);
            //                 return UnityEditor.AssetDatabase.LoadAssetAtPath(realPath, type);
            //             }
            // #else
            //             return BundleManager.Ins.LoadAssetFromBundle(path, type);
            // #endif
        }

        public UnityEngine.Object LoadAsset(string path, bool autoUnload = false)
        {
            return LoadAsset<UnityEngine.Object>(path, autoUnload);
        }

        public TextAsset LoadText(string path, bool autoUnload = false)
        {
            return LoadAsset<TextAsset>(path, autoUnload);
        }

        public GameObject LoadPrefab(string path, bool autoUnload = false)
        {
            return LoadAsset<GameObject>(path, autoUnload);
        }

        public Texture LoadTexture(string path, bool autoUnload = false)
        {
            return LoadAsset<Texture>(path, autoUnload);
        }

        public void PreLoadCommonRes()
        {

        }

        public void PreLoadSceneRes(string sceneName)
        {

        }

        public void UnLoadBundle(string bundleName)
        {

        }
    }

//    public static class ResLoader2
//    {
//        private class ResLoaderHelper
//        {
//            private Dictionary<string, Dictionary<string, string>> ext2name2path = new Dictionary<string, Dictionary<string, string>>();
//            public void Init()
//            {
//                var ext2name2path_path = FileHelper.CombinePath(FileHelper.EditorProjectDir, HotFixConfigs.MiddleWarePath);
//                ext2name2path_path = FileHelper.CombinePath(ext2name2path_path, HotFixConfigs.Ext2File2PathFile);
//                var ext2name2path_text = FileHelper.FileReadAllText(ext2name2path_path);
//                if (ext2name2path_text == null || ext2name2path_text.Length == 0)
//                {
//                    HotFixMain.TriggerWarning("cant load Ext2Name2Path File");
//                    return;
//                }
//                ext2name2path = LitJson.JsonMapper.ToObject<Dictionary<string, Dictionary<string, string>>>(ext2name2path_text);
//            }

//            public string GetFilePathByName(string name, string ext)
//            {
//                Dictionary<string, string> name2path;
//                if(!ext2name2path.TryGetValue(ext, out name2path))
//                {
//                    return null;
//                }

//                string path;
//                if(!name2path.TryGetValue(name, out path))
//                {
//                    return null;
//                }
//                return path;
//            }
//        }
//        private static ResLoaderHelper loaderHelper;
//        public static void InitResources()
//        {
//            if(HotFixMain.Config.EnableHotFix)
//            {
//                BundleManager.Instance.Init();
//            }
//            //else if(HotFixMain.Config.EnableResLoadByName)
//            //{
//            //    loaderHelper = new ResLoaderHelper();
//            //    loaderHelper.Init();
//            //}
//            HotFixMain.Trigger.InvokeTrigger(TriggerType.InitNext);
//        }

//        private static string GetPath(string name, string ext)
//        {
//            string path;
//#if UNITY_EDITOR
//            if(HotFixMain.Config.EnableHotFix)
//            {
//                path = BundleManager.Ins.GetFilePathByName(name, ext);
//            }
//            else
//            {
//                path = loaderHelper.GetFilePathByName(name, ext);
//            }
//#else
//            path = BundleManager.Instance.GetFilePathByName(name, ext);
//#endif
//            if(path == null)
//            {
//                throw new Exception("Bundle resource: " + name + "." + ext + " cant be found");
//            }
//            return path;
//        }

//        public static UnityEngine.Object LoadAsset(string path, string ext = "NoExt", bool autoUnload = false)
//        {
//            return ABResLoader.LoadAsset<UnityEngine.Object>(GetPath(path, ext), autoUnload);
//        }

//        public static TextAsset LoadText(string path, string ext = ".bytes", bool autoUnload = false)
//        {
//            return ABResLoader.LoadAsset<TextAsset>(GetPath(path, ext), autoUnload);
//        }

//        public static GameObject LoadPrefab(string path, bool autoUnload = false)
//        {
//            return ABResLoader.LoadAsset<GameObject>(GetPath(path, ".prefab"), autoUnload);
//        }

//        public static Texture LoadTexture(string path, string ext = ".png", bool autoUnload = false)
//        {
//            return ABResLoader.LoadAsset<Texture>(GetPath(path, ext), autoUnload);
//        }
//    }
}

